package com.zx.framework.plat.param.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zx.framework.core.pagination.Pagination;
import com.zx.framework.core.util.BeanUtil;
import com.zx.framework.core.util.RandomUtil;
import com.zx.framework.core.util.StringUtil;
import com.zx.framework.core.util.layer.LayerModle;
import com.zx.framework.core.util.layer.LayerUtil;
import com.zx.framework.plat.param.po.Param;
import com.zx.framework.plat.param.service.ParamService;
import com.zx.framework.plat.param.vo.ParamVo;
import com.zx.framework.web.controller.BaseController;
import com.zx.framework.web.exception.AjaxException;
import com.zx.framework.web.exception.UIException;

@Controller
@RequestMapping("/param")
public class ParamController extends BaseController {

    private static final Logger LOGGER = Logger.getLogger(ParamController.class);
    @Resource
    private ParamService paramService;

    @InitBinder("param")
    public void paramBinder(WebDataBinder binder) throws Exception {
        binder.setFieldDefaultPrefix("param.");
    }

    /**
     * 分页查询方法
     * @param param   查询模板
     * @param pagination    分页
     * @return              分页模板路径
     */
    @RequestMapping("/page")
    public String query( Param param, Pagination<Param> pagination,Model model) {
    	try{
			model.addAttribute("param", param);
        	model.addAttribute("pagination", paramService.pageQuery(param, pagination.getPage(), pagination.getRows()));
		}catch (Exception e) {
			throw new UIException("获取参数列表信息失败！");
		}
        return "param/list";
    }
    /**
     * 分页查询方法
     * @param param   查询模板
     * @param pagination    分页
     * @return              分页模板路径
     */
    @RequestMapping("/ajax/page")
    @ResponseBody
    public String ajaxQuery( Param param,  
    		@RequestParam(required = false, defaultValue = "10") Integer rows,
            @RequestParam(required = false, defaultValue = "1") Integer page) {
    	try{
    		
		}catch (Exception e) {
			throw new UIException("获取参数列表信息失败！");
		}
        return "param/list";
    }
    /**
     * 获取所有参数的json
     * @return
     */
    @RequestMapping("/query/all")
    @ResponseBody
    public Object queryAll(){
    	try {
			List<Param> params = paramService.findAll("layer");
			Map m = request.getParameterMap();
			return clientMap().success().message("获取成功！").datas(params).getMap();
		} catch (Exception e) {
			LOGGER.error("",e);
			throw new AjaxException(e.getMessage());
		}
    }
    
    /**
     * 根据ID获取参数信息
     * @param id
     * @return
     */
    @RequestMapping("/query/id/{id:.+}")
    @ResponseBody
    public Object queryParamById(@PathVariable("id") String id){
    	try {
			Param param = paramService.findParamById(id);
			if(param == null){
				throw new AjaxException("参数信息不存在！");
			}
			return clientMap().success().message("获取成功！").put("param",param).getMap();
		}catch (AjaxException e) {
			throw e;
		}catch (Exception e) {
			LOGGER.error("",e);
			throw new AjaxException("获取参数信息失败！");
		}
    }

    @RequestMapping("/add/save")
    @ResponseBody
    public Map<String, Object> addSave(ParamVo param) {
        if (param == null) {
            throw new AjaxException(getMessage("e.save.po.null"));
        }
        try {
			validate(param);
		} catch (Exception e) {
			throw new AjaxException(e.getMessage());
		}
        try{
        	if(StringUtil.isNotEmpty(param.getOwner())){
        		Param parent = paramService.findParamById(param.getOwner());
        		if(parent == null){
        			throw new AjaxException(getMessage("e.object.parent.notexist"));
        		}
        	}
        	Param p = paramService.findParamById(param.getId());
        	if(p!=null){
        		throw new AjaxException(getMessage("e.object.not.null", "ID",param.getId()));
        	}
        	Param saveParam = new Param();
        	BeanUtil.copyProperties(param, saveParam);
        	paramService.saveParam(saveParam);
        	return clientMap().success().message(getMessage("op.ok")).put("param", param).getMap();
		}catch (Exception e) {
			throw new AjaxException(getMessage("e.param.find.error")+e);
		}
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(@RequestParam(value="ids",required=false) List<String> ids){
        if (ids==null || ids.isEmpty()) {
            throw new AjaxException("错误的请求参数！");
        }
        
        Map<String, Object> rs = new HashMap<String, Object>();
        rs.put("status", HttpStatus.OK.value());
        rs.put("message", "删除参数成功！");
        return rs;
    }

    @RequestMapping("edit/save")
    @ResponseBody
    public Map<String, Object> editSave(Param param) {
    	if (param == null) {
            throw new AjaxException(getMessage("e.save.po.null"));
        }
        try {
			validate(param);
		} catch (Exception e) {
			throw new AjaxException(e.getMessage());
		}
        try{
        	if(StringUtil.isNotEmpty(param.getOwner())){
        		Param parent = paramService.findParamById(param.getOwner());
        		if(parent == null){
        			throw new AjaxException(getMessage("e.object.parent.notexist"));
        		}
        	}
        	Param p = paramService.findParamById(param.getId());
        	if(p==null){
        		throw new AjaxException(getMessage("e.save.po.null"));
        	}
        	
        	BeanUtil.copyProperties(param, p);
        	paramService.updateParam(p);
        	return clientMap().success().message(getMessage("op.ok")).put("param", param).getMap();
		}catch (Exception e) {
			throw new AjaxException(getMessage("e.param.find.error")+e);
		}
    }
    
    /*附加的其他业务跳转*/
}
